package servlet;

import pojo.ShangHanTiaoBian;
import service.ShtbService;
import util.PageUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(urlPatterns = "/ShtbServlet")
public class ShtbServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    ShtbService service = new ShtbService();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        request.setCharacterEncoding("utf-8");
        String action = request.getParameter("action");
        if("list".equals(action)) { //列表展示
            int curPage=Integer.parseInt(request.getParameter("curPage"));
            request.setAttribute("curPage", curPage);
            PageUtil<ShangHanTiaoBian> pageUtil= service.getShtbList(curPage);
            request.setAttribute("shtbList",pageUtil);
            request.getRequestDispatcher("/WEB-INF/jsp/ShtbList.jsp?curPage=1").forward(request,response);
        }else if("delete".equals(action)){ //删除指定id的条辩
            request.setCharacterEncoding("UTF-8");
            String idtemp = request.getParameter("id");
            int id = Integer.parseInt(idtemp);
            service.deleteShtb(id);
            response.sendRedirect("ShtbServlet?action=list&curPage=1");
        }else if ("editUI".equals(action)){//编辑界面展示
            String idtemp = request.getParameter("id");
            int id = Integer.parseInt(idtemp);
            ShangHanTiaoBian shtbedit = service.getShtb(id);
            request.setAttribute("edit",shtbedit);
            request.getRequestDispatcher("/WEB-INF/jsp/ShtbEdit.jsp").forward(request,response);
        }else if ("editproc".equals(action)){//编辑处理界面
            request.setCharacterEncoding("UTF-8");
            int id = Integer.parseInt(request.getParameter("id"));
            int num = Integer.parseInt(request.getParameter("num"));
            String content = request.getParameter("content");
            String comment = request.getParameter("comment");

            ShangHanTiaoBian shtb = new ShangHanTiaoBian();
            shtb.setId(id);
            shtb.setNum(num);
            shtb.setContent(content);
            shtb.setComment(comment);
            service.updateShtb(shtb);
            response.sendRedirect("ShtbServlet?action=list&curPage=1");
        }else if ("addUI".equals(action)) {//添加界面
            request.getRequestDispatcher("/WEB-INF/jsp/ShtbAdd.jsp").forward(request,response);

        }else if ("addproc".equals(action)){//添加处理
            int num = Integer.parseInt(request.getParameter("num"));
            String content = request.getParameter("content");
            String comment = request.getParameter("comment");

            ShangHanTiaoBian shtb = new ShangHanTiaoBian();
            shtb.setNum(num);
            shtb.setContent(content);
            shtb.setComment(comment);
            service.addShtb(shtb);
            response.sendRedirect("ShtbServlet?action=list&curPage=1");
        }else if("search".equals(action)){//搜索处理
            String key=request.getParameter("key");
            int flag=Integer.parseInt(request.getParameter("flag"));
            PageUtil pageUtil = service.getQueryShtbList(key,flag);
            request.setAttribute("shtbList", pageUtil);
            request.setAttribute("curPage", 1);
            request.getRequestDispatcher("/WEB-INF/jsp/ShtbList.jsp?curPage=1").forward(request, response);
        }else if("detail".equals(action)){//细节展示
            request.setCharacterEncoding("UTF-8");
            int tbid=Integer.parseInt(request.getParameter("tbid"));
            request.setAttribute("detail",service.getShtbDetail(tbid));
            request.getRequestDispatcher("/WEB-INF/jsp/ShtbDetail.jsp").forward(request, response);
        }
    }

    }